package com.mobilemoney.mm.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobilemoney.mm.R;
import com.mobilemoney.mm.SecurityContentActivity;

public class SecurityBaseAdapter extends BaseAdapter {

	private String[] securitytitle = null;
	private String[] securitycontent = null;
	private int[] imageViewIds = new int[]{
			R.drawable.securitypay,
			R.drawable.securitypassword,
			R.drawable.securitymobile
	};
	private LayoutInflater layoutInflater;
	
	Activity activity;

	public SecurityBaseAdapter(Activity activity) {
		this.activity = activity;
		layoutInflater = LayoutInflater.from(activity.getApplicationContext());
		
		securitytitle = activity.getResources().getStringArray(R.array.securitytitle);
		securitycontent = activity.getResources().getStringArray(R.array.securitycontent);
	}
	
	@Override
	public int getCount() {
		return securitycontent.length;
	}

	@Override
	public Object getItem(int position) {
		return securitycontent[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		TextView securitytitle;
		ImageView securityimageView;
		TextView securitycontent;
		TextView securityalink;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.viewpager2item, null);
			viewHolder = new ViewHolder();
			viewHolder.securitytitle = (TextView) convertView.findViewById(R.id.securitytitle);
			viewHolder.securityimageView = (ImageView) convertView.findViewById(R.id.securityimageView);
			viewHolder.securitycontent = (TextView) convertView.findViewById(R.id.securitycontent);
			viewHolder.securityalink = (TextView) convertView.findViewById(R.id.securityalink);
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
			
		}

		viewHolder.securitytitle.setText(securitytitle[position]);
		viewHolder.securityimageView.setImageResource(imageViewIds[position]);
		viewHolder.securitycontent.setText(securitycontent[position]);
		viewHolder.securityalink.setText(R.string.securityalink);
		
		viewHolder.securityalink.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				System.out.println("alink click!!!");
				Intent intent = new Intent();
				intent.setClass(activity, SecurityContentActivity.class);
				intent.putExtra("position", position);
				activity.startActivity(intent);
			}
		});
		viewHolder.securityimageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				System.out.println("btn click1!!!");
				Intent intent = new Intent();
				intent.setClass(activity, SecurityContentActivity.class);
				intent.putExtra("position", position);
				activity.startActivity(intent);
			}
		});
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				System.out.println("item  click1!!!");
				Intent intent = new Intent();
				intent.setClass(activity, SecurityContentActivity.class);
				intent.putExtra("position", position);
				activity.startActivity(intent);
			}

		});
		return convertView;
	}

}
